package com.zy;

import dev.langchain4j.model.input.Prompt;
import dev.langchain4j.model.input.PromptTemplate;
import dev.langchain4j.model.input.structured.StructuredPrompt;
import dev.langchain4j.model.input.structured.StructuredPromptProcessor;
import dev.langchain4j.model.openai.OpenAiChatModel;

import java.util.List;
import java.util.Map;

import static java.time.Duration.ofSeconds;

/**
 * @program: AI_langchain4j
 * @description: 提示词模板: 在提示词中用占位符将一个可变的数据嵌入到提示词中
 * @author: zy
 * @create: 2025-06-28 15:01
 */
public class _06_PromptTemplate {

    //1. 简单模板
    static class Simple_Prompt_Template_Example{
        public static void main(String[] args) {
            String apiKey = System.getenv("OPEN_AI_KEY");

            OpenAiChatModel model = OpenAiChatModel.builder()
                    .apiKey(apiKey)
                    .modelName("deepseek-chat")
                    .baseUrl(   "https://api.deepseek.com"    )    //deepseek的API地址
                    .logRequests(true)
                    .logResponses(true)
                    .timeout(ofSeconds(60))
                    .build();
            // dishType: 菜的类型
            //  ingredients: 菜的原料(多个，逗号分隔
            String template="Create a recipe for a {{dishType}} with the following ingredients: {{ingredients}}";
            PromptTemplate promptTemplate= PromptTemplate.from( template );
            // 填充变量
            Map<String, Object> variables=new java.util.HashMap<>();
            variables.put("dishType", "oven dish");
            variables.put("ingredients", "chicken, rice, vegetables, potato, tomato");
            // 生成提示词
            Prompt prompt=promptTemplate.apply(   variables   );

            String response= model.chat(    prompt.text() );
            System.out.println(response);
        }
    }

    //2. 复杂模板
    static class Complex_Prompt_Template_Example {

        @StructuredPrompt({
                "create a recipe of a {{dish}} that can be prepared using only {{ingredients}}. ",
                "Structure your answer in the following way:",
                "Recipe name:....",
                "Description:...",
                "Preparation time:....",
                "Required ingredients:....",
                "Instructions:...."
        })
        static class CreateRecipePrompt{
            String dish;
            List<String> ingredients;

            CreateRecipePrompt(String dish, List<String> ingredients) {
                this.dish = dish;
                this.ingredients = ingredients;
            }
        }


        public static void main(String[] args) {
            String apiKey = System.getenv("OPEN_AI_KEY");

            OpenAiChatModel model = OpenAiChatModel.builder()
                    .apiKey(apiKey)
                    .modelName("deepseek-chat")
                    .baseUrl(   "https://api.deepseek.com"    )    //deepseek的API地址
                    .logRequests(true)
                    .logResponses(true)
                    .timeout(ofSeconds(60))
                    .build();
            CreateRecipePrompt crPrompt= new CreateRecipePrompt("oven dish", List.of("chicken", "rice", "potato", "tomato"));
            Prompt prompt=StructuredPromptProcessor.toPrompt(   crPrompt );
            String response= model.chat(   prompt.text() );
            System.out.println(response);
        }
    }
}
